package com.jianyushe.common.util;

import org.apache.poi.ss.usermodel.Cell;
import org.apache.poi.ss.usermodel.Row;
import org.apache.poi.ss.usermodel.Sheet;
import org.apache.poi.ss.usermodel.Workbook;
import org.apache.poi.xssf.streaming.SXSSFWorkbook;
import org.joda.time.DateTime;
import org.junit.Test;

import java.io.FileOutputStream;

/**
 * @author Create by Alvin
 * @Description TODO
 * @date 2022-04-18 21:21
 */
public class PoiWriteExcelDemo {

    private String path = "F:\\Temp\\";
    @Test
    public void ExcelTest() throws Exception {
        // 创建工作薄
        Workbook workbook = new SXSSFWorkbook();
        // 创建工作表sheet
        Sheet sheet = workbook.createSheet();
        // 创建行
        Row row = sheet.createRow(1);
        // 创建列 第一列
        Cell cell = row.createCell(1);
        cell.setCellValue("poi ooxml 创建");

        // 第二列
        Cell cell1 = row.createCell(2);
        String l = new DateTime().toString("yyyy-MM-dd HH:mm:ss");
        cell1.setCellValue(l);

        // 创建文件流，用于输出文件
        FileOutputStream fileOutputStream = new FileOutputStream(path + "test07版本.xlsx");
        // 输出
        workbook.write(fileOutputStream);
        // 关闭流
        fileOutputStream.close();
        // 清除临时文件
      //  workbook.dispose();
        System.out.println("文件生成完毕");
    }
}
